﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Hangout.Web.Services.Score
{
    public class Score
    {
         Web.Model.HangoutDBEntities context = new Web.Model.HangoutDBEntities();

        public  Web.Model.UserScore IncreaseUserScore(int userId, int scoreInc, string accesstoken)
        {
            try
            {
                if (Web.Core.Accounts.User.IsValid(userId, accesstoken, context))
                {
                    return Web.Core.Score.UserScore.IncreaseUserScore(userId, scoreInc, ref context);
                }
                else { throw new Web.Core.Exceptions.Account.UnAuthorisedAccess(); }
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(userId, Web.Core.ClientType.WindowsAzure, ex, ref context);
                return null;
            }
        }

        public  Web.Model.UserScore DecreaseUserScore(int userId, int scoreDec, string accesstoken)
        {
            try
            {
                if (Web.Core.Accounts.User.IsValid(userId, accesstoken, context))
                {
                    return Web.Core.Score.UserScore.DecreaseUserScore(userId, scoreDec, ref context);
                }
                else { throw new Web.Core.Exceptions.Account.UnAuthorisedAccess(); }
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(userId, Web.Core.ClientType.WindowsAzure, ex, ref context);
                return null;
            }

        }

        public  int? GetLastScore(int userId)
        {
            try
            {
                
                    Web.Model.UserScore score = Web.Core.Score.UserScore.GetLastScore(userId, ref context);
                    if (score != null)
                    {
                        return score.CurrentScore;
                    }

                    return null;
                
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(userId, Web.Core.ClientType.WindowsAzure, ex, ref context);
                return null;
            }
        }

        public  Web.Model.UserScore InsertDefaultScore(int userId, string accesstoken)
        {
            try
            {
                if (Web.Core.Accounts.User.IsValid(userId, accesstoken, context))
                {
                    return Web.Core.Score.UserScore.InsertDefaultScore(userId, ref context);
                }
                else { throw new Web.Core.Exceptions.Account.UnAuthorisedAccess(); }
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(userId, Web.Core.ClientType.WindowsAzure, ex, ref context);
                return null;
            }
        }

        public  Web.Model.UserScore InsertScore(int userId, int score, int scoreChange, string accesstoken)
        {
            try
            {
                if (Web.Core.Accounts.User.IsValid(userId, accesstoken, context))
                {
                    return Web.Core.Score.UserScore.InsertScore(userId, score, scoreChange, ref context);
                }
                else { throw new Web.Core.Exceptions.Account.UnAuthorisedAccess(); }
            }
            catch (System.Exception ex)
            {
                Web.Core.Exceptions.ExceptionReporting.AddAnException(userId, Web.Core.ClientType.WindowsAzure, ex, ref context);
                return null;
            }
        }
    }
}